<?php
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1;
// connect to the database
include "../../includes/functions.php";

echo db_connect(); 
 
$result = mysql_query("SELECT COUNT(*) id FROM sponsorship_spots");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

if( $count>0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages =		 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
//$SQL = "SELECT * FROM sponsorship_spots ORDER BY $sidx $sord LIMIT $start , $limit"; echo $SQL;

$SQL = "SELECT * FROM sponsorship_spots ORDER BY track_number, title"; //echo $SQL;
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());

//$sched_SQL = "SELECT count (id) FROM sponsorship_schedules WHERE id = ''"; //echo $SQL;
//$sched_result = mysql_query( $sched_SQL ) or die("Couldnt execute query.".mysql_error());

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";

echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
	echo "<row id='". $row[id]."'>";
	echo "<cell>". $row[id]."</cell>";
	echo "<cell>". $row[track_number]."</cell>"; 
	echo "<cell>". replaceChars($row[title])."</cell>";
	echo "<cell>". replaceChars($row[text])."</cell>"; 
	echo "<cell>". $row[type]."</cell>";
	echo "<cell>". $row[start_date]."</cell>"; 
	echo "<cell>". $row[end_date]."</cell>"; 

	//echo "<cell><![CDATA[". $row[status]."]]></cell>";
	//echo "<cell>". $row[status]."</cell>";
	echo "<cell></cell>";
	
	echo "</row>";
}
echo "</rows>";		

function replaceChars($string) {
	 //$string=str_replace("'","",$string);
	 $string=str_replace("&","and",$string);
	 $string=str_replace('"',"'",$string);
	//$string=str_replace("-","",$string);
	 
	return $string;
	
}

?>	
